/*
从store(cache)取得基本数据
组件使用computed,响应store的数据变化
computed: {
    regionIdOptions() {
      return getLimitRegions()
    }
  },
*/
import store from '@/store'

export const processKeys = [{ id: 'award', name: '荣誉' }, { id: 'training', name: '学习交流' }, { id: 'socialJob', name: '社会任职' }, { id: 'publish', name: '教师发表' }, { id: 'leave', name: '员工请假' }]
/**
 * 缓存更新时间
 */
const baseDataLoaded = {
  limitData: null,
  deptTree: null,
  companyTree: null
}
/**
 * 判断缓存是否失效
 * @param {*} diff 失效时间差，默认30分钟=1800s
 */
const isCacheFresh = (cacheTimestemp, diff = 1800) => {
  if (cacheTimestemp) {
    return ((Date.now() - cacheTimestemp) / 1000) > diff
  } else {
    return true
  }
}
export function resetCache(part) {
  if (part === 'department') {
    baseDataLoaded.companyTree = null
    baseDataLoaded.limitData = null
    baseDataLoaded.deptTree = null
  } else if (part === 'dirtionary') {
    baseDataLoaded.putinTypes = null
  }
}

/**
 * 部门树
 */
export function getDeptTree() {
  if (isCacheFresh(baseDataLoaded.deptTree)) {
    store.dispatch('basedata/setDeptTree')
    baseDataLoaded.deptTree = Date.now()
  }
  // return store
  return store.getters.deptTree
}
/**
 * 公司树
 */
export function getComapnyTree() {
  if (isCacheFresh(baseDataLoaded.companyTree)) {
    store.dispatch('basedata/setCompanyTree')
    baseDataLoaded.companyTree = Date.now()
  }
  // return store
  return store.getters.companyTree
}

